package com.example.zhaojianxu.signin.car.leasecar

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.View
import com.example.zhaojianxu.signin.R
import com.example.zhaojianxu.signin.bean.BaseActivity
import com.example.zhaojianxu.signin.car.http.CarEntity
import com.example.zhaojianxu.signin.common.http.HttpUtil
import kotlinx.android.synthetic.main.activity_lease_car.*
import kotlinx.android.synthetic.main.common_title_bar.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class LeaseCarActivity : BaseActivity() {

    var mAdapter: LeaseCarAdapter? = null

    override fun initView() {
        common_back.setOnClickListener {
            finish()
        }
        common_setting.visibility = View.GONE
        common_title.text = "租车列表"

        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        activity_lease_car_2.layoutManager = layoutManager

        mAdapter = LeaseCarAdapter()
        activity_lease_car_2.adapter = mAdapter

        activity_lease_car_swipe.setOnRefreshListener {
            initData()
        }
    }

    override fun initData() {

        HttpUtil.instance.getCarApi()?.postQueryCar()?.enqueue(object : Callback<MutableList<CarEntity>>{
            override fun onFailure(call: Call<MutableList<CarEntity>>, t: Throwable) {
            }

            override fun onResponse(call: Call<MutableList<CarEntity>>, response: Response<MutableList<CarEntity>>) {
                val list = response.body()
                if (response.isSuccessful && list != null){

                    var temp = mutableListOf<CarEntity>()

                    for (position in 0 until list.size){
                        if (list[position].number > 0){
                            temp.add(list[position])
                        }
                    }

                    mAdapter?.mList = temp
                    mAdapter?.notifyDataSetChanged()
                    activity_lease_car_swipe.isRefreshing = false
                }
            }

        })


    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_lease_car)
    }
}
